package com.fourth.shopping.controller;



import com.fourth.shopping.commons.Code;
import com.fourth.shopping.commons.TableData;
import com.fourth.shopping.dto.UserTableSearch;
import com.fourth.shopping.entity.User;
import com.fourth.shopping.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class UserController {
    @Resource
    UserService userService;



    @GetMapping("/selectAllUser.do")
    public List<User> selectAllUser() {
        return userService.selectAllUser();
    }



    @GetMapping("/getPageData.do")
    public TableData<User> getPageDate(UserTableSearch userTableSearch, @RequestParam("offset") Integer beginIndex, @RequestParam("limit") Integer pageSize, HttpSession session) {

        System.out.println(userTableSearch);

        System.out.println(session);
        return userService.getPageDate(userTableSearch, beginIndex, pageSize);

    }




    @GetMapping("/activeUser.do")
    public Code activeUser(String userId){


        Integer integer = userService.changeStatus(userId, 1);
        if (integer > 0) {
            return new Code(integer,"success");
        } else {
            return new Code(integer,"defeated");
        }

    }

    @GetMapping("/logOutUser.do")
    public Code logOutUser(String userId){

        Map<String, Object> code =  new HashMap<>();

        Integer integer = userService.changeStatus(userId, 0);
        if (integer > 0) {
            return new Code(integer,"success");
        } else {
            return new Code(integer,"defeated");
        }

    }






}
